<?php

/** 
 * 合并用户操作纪录模型
 */
class User_combine_log_mdl extends Mongo_Model {
	
	/**
	 */
	public function __construct() {
		$this->collName = 'UserCombineLog';
		parent::__construct ();
	}
	/**
	 * 纪录一次修改记录
	 * @param  $mainUserDetail 主账户数据
	 * @param  $delUserDetail 废弃账户数据
	 * @param  $IdListForChangedTables 被改变的表的Id列表
	 * @return bool
	 */
	public function recode($mainUserDetail,$delUserDetail,$IdListForChangedTables){
		$mainUserDetail = $this->_formatUser($mainUserDetail);
		$delUserDetail = $this->_formatUser($delUserDetail);
		
		foreach ($IdListForChangedTables as $table=>$list){
			$IdListForChangedTables[$table] = array_map(function($v){
				return (int)$v;
			}, $list);
		}
		
		$where = array('_id'=>(int)$mainUserDetail['id']);
		$data  = array(
				'mainUser'=>$mainUserDetail,
				'delUser'=>$delUserDetail,
				'tables'=>$IdListForChangedTables,
		);
		debug('recode',$data);
		$update = array(
				'$push'=>array('list'=>$data),
		);
		$options = array('upsert'=>true);
		$res = $this->update($where, $update,$options);
		return $res['ok']==1;
	}
	
	private function _formatUser($user){
		$intFields = array('id','balance','isDel','schoolID','isTest','rechargeTotal','buyTotal','presrent','combineID');
		while ( (list($key,$val) = each($user)) ) {
			if ( in_array($key,$intFields ) ) {
				$user[$key] = (int)$val;
			}
		}
		return $user;
	}
	
	
}

